//
//  NetNewsViewController.m
//  BaseTools
//
//  Created by 李挺哲 on 15/12/10.
//  Copyright © 2015年 ltz. All rights reserved.
//

#import "NetNewsViewController.h"
#import "FXBlurView.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define DialogViewHeight  ScreenHeight*0.3
@interface NetNewsViewController ()
{
    FXBlurView *maskView;
    UITextView * commentView;
    UIView *dialogView;
    UIView *blackView;
}

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
@end

@implementation NetNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURL * url=[[NSURL alloc] initWithString:@"http://www.baidu.com"];
    NSURLRequest *request=[[NSURLRequest alloc] initWithURL:url];
    [_webView loadRequest:request];
    
    NSLog(@"%@",NSStringFromCGRect(_toolBar.frame));
    UIButton * comment=[[UIButton alloc] initWithFrame:CGRectMake(30, 3, ScreenWidth-60, _toolBar.frame.size.height-6)];
    //    comment.backgroundColor=[UIColor redColor];
    UIImage *image=[UIImage imageNamed:@"toolbar_light_comment"];
    NSInteger leftCapWidth = image.size.width * 0.5f;
    // 顶端盖高度
    NSInteger topCapHeight = image.size.height * 0.5f;
    // 重新赋值
    image = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
    [comment setBackgroundImage:image forState:UIControlStateNormal];
    [comment addTarget:self action:@selector(addComment) forControlEvents:UIControlEventTouchUpInside];
    
    [_toolBar addSubview:comment];
    
    maskView=[[FXBlurView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    maskView.blurRadius=2.0f;
    maskView.tintColor=[UIColor clearColor];
    [maskView clearImage];
    dialogView=[[UIView alloc] init];
    dialogView.frame=CGRectMake(0, ScreenHeight, ScreenWidth, DialogViewHeight);
    dialogView.backgroundColor=[UIColor lightGrayColor];
    [maskView addSubview:dialogView];
    commentView=[[UITextView alloc] init];
    
    commentView.frame=CGRectMake(0, 0, ScreenWidth*0.6, DialogViewHeight/2);
    commentView.backgroundColor=[UIColor whiteColor];
    commentView.center=CGPointMake(ScreenWidth/2, DialogViewHeight/2);
    [dialogView addSubview:commentView];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped)];
    
    [self.view addGestureRecognizer:singleTap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    

    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    
    
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    
    
}
-(void)addComment{
    blackView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    blackView.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    [self.view addSubview:blackView];
    [self.view addSubview:maskView];
    [commentView becomeFirstResponder];
    
}

-(void)fingerTapped{
    
    [blackView removeFromSuperview];
    [maskView removeFromSuperview];
    [commentView resignFirstResponder];
    dialogView.frame=CGRectMake(0, ScreenHeight, ScreenWidth, DialogViewHeight);
}

- (void)removeKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    
}

- (void)keyboardWillShow:(NSNotification *)notification{
    
    CGRect keyboardBounds;
    [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
    NSLog(@"keyboadr  %@",NSStringFromCGRect(keyboardBounds));
    int  keyBoardHeight=keyboardBounds.size.height;
    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    
    dialogView.center=CGPointMake(ScreenWidth/2, ScreenHeight-keyBoardHeight-DialogViewHeight/2);
    NSLog(@"dialogView  frame %@",NSStringFromCGRect(dialogView.frame));
    NSLog(@"comment  frame %@",NSStringFromCGRect(commentView.frame));
    [UIView commitAnimations];
    
    
    
    
}



-(void)dealloc{
    
    [self removeKeyboardNotifications];
    
    
}



@end
